package naga.x.platform2.channel.wanba.support;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import naga.x.platform2.App;
import naga.x.platform2.utils.NumberUtils;
import naga.x.platform2.utils.StringUtils;

@Configuration
@EnableAutoConfiguration
@ConfigurationProperties(locations = "classpath:qq_wanba.properties")
public class QZoneConfig implements InitializingBean {

	private String appId;
	private String appKey;
	private String host;
	private String protocol;
	private String androidRecharges;
	private String iosRecharges;
	
	private Map<Integer, Integer> androidRechargeItems, iosRechargeItems;

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getProtocol() {
		return protocol;
	}

	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getAndroidRecharges() {
		return androidRecharges;
	}

	public void setAndroidRecharges(String androidRecharges) {
		this.androidRecharges = androidRecharges;
	}

	public String getIosRecharges() {
		return iosRecharges;
	}

	public void setIosRecharges(String iosRecharges) {
		this.iosRecharges = iosRecharges;
	}
	
	@Override
	public String toString() {
		return "[appId=" + appId + ", appKey=" + appKey + ", host=" + host + ", protocol=" + protocol
				+ ", androidRechargeItems=" + androidRechargeItems + ", iosRechargeItems=" + iosRechargeItems + "]";
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		androidRechargeItems = parseRechargeItems(androidRecharges);
		iosRechargeItems = parseRechargeItems(iosRecharges);
		QZoneApi.init(this);
		App.LOG.info("[QQWB CONFIG LOADED]{}", this);
	}
	
	private static Map<Integer, Integer> parseRechargeItems(String text) {
		Map<Integer, Integer> map = new HashMap<>();
		String[] strs = StringUtils.split(text, ",");
		for (String str : strs) {
			int n = str.indexOf('=');
			int key = NumberUtils.parseInt(StringUtils.trim(str.substring(0, n)));
			int val = NumberUtils.parseInt(StringUtils.trim(str.substring(n + 1)));
			if (key == 0 || val == 0) {
				throw new RuntimeException("qq_wanba config err: " + text);
			}
			map.put(key, val);
		}
		return map;
	}
	
	public Integer getRechargeItemByProduct(String zoneid, Integer product) {
		return QZoneApi.ZONEID_ANDROID.equals(zoneid) ? androidRechargeItems.get(product) : iosRechargeItems.get(product);
	}

}
